package cn.e4j.modular.crm.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.common.exception.BizExceptionEnum;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.AppCxt;
import cn.e4j.core.util.LangUtils;
import cn.e4j.core.util.StringUtils;
import cn.e4j.modular.crm.entity.GwcsT;
import cn.e4j.modular.crm.entity.GwcszT;
import cn.e4j.modular.crm.service.GwakTService;
import cn.e4j.modular.crm.service.GwamTService;
import cn.e4j.modular.crm.service.GwcsTService;
import cn.e4j.modular.crm.service.impl.GwcsTServiceImpl;
import cn.e4j.modular.erp.controller.ErpLogic;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * 正式订单维护管理
 * Created by shixc on 2019-1-11.
 */
@Controller
@RequestMapping("/crm/gwai710")
public class Gwai710Controller extends BaseController {

    @Autowired
    private GwamTService gwamTService;

    @Autowired
    private GwcsTService gwcsTService;

    @Autowired
    private GwcsTServiceImpl gwcsTServiceImpl;

    @Autowired
    private GwakTService gwakTService;

    @Autowired
    private ErpLogic erpLogic;



    /**
     * 跳转正式订单管理页面
     */
    @RequiresPermissions("crm:gwai710:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwai710.html";
    }

    /**
     * 查询正式订单信息列表
     */
    @RequestMapping("/getList")
    @RequiresPermissions("crm:gwai710:init")
    @ResponseBody
    public ResponseData getList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto inDto = dtoWapper.getDto();
        inDto.put("GWCSENT", shiroUser.getDBC());
        inDto.put("lang", LangUtils.getLang(shiroUser.getLANG()));
        List<String> list = null;
        if (!StringUtils.isEmpty(inDto.getString("GWCSACTI_LIST"))) {
            list = new ArrayList<>();
            String[] str = inDto.getString("GWCSACTI_LIST").split(",");
            for (String GWCSACTI : str) {
                list.add(GWCSACTI);
            }
            inDto.put("list", list);
        }
        List<Map<String, Object>> result = gwcsTService.getList(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 查询正式订单信息列表
     */
    @RequestMapping("/listGwcs")
    @RequiresPermissions("crm:gwai710:init")
    @ResponseBody
    public ResponseData listGwcs(@DtoParam DtoWapper dtoWapper) {
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto inDto = dtoWapper.getDto();
        inDto.put("GWCSENT", shiroUser.getDBC());
        List<Map<String, Object>> result = gwcsTService.listGwcs(inDto);
        return ResponseData.success(result);
    }

    /**
     * 新增
     */
    @RequestMapping("/addData")
    @RequiresPermissions("crm:gwai710:init")
    @ResponseBody
    public ResponseData addData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwcsT> gwcsTList = inDto.getReqList(GwcsT.class);
        gwcsTService.addData(gwcsTList);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/editData")
    @RequiresPermissions("crm:gwai710:init")
    @ResponseBody
    public ResponseData editData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwcsT> gwcsTList = inDto.getReqList(GwcsT.class);
        gwcsTService.editData(gwcsTList);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwai710:init")
    @ResponseBody
    public ResponseData del(GwcsT gwcsT) {
        gwcsTService.delete(gwcsT);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/deleteDataBatch")
    @RequiresPermissions("crm:gwai710:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwcsT> gwcsTList = inDto.getReqList(GwcsT.class);
        gwcsTService.delDataBatch(gwcsTList);
        return SUCCESS_TIP;
    }

    /**
     * 获取新增正式单号
     */
    @RequestMapping("/getGwcsdocno")
    @RequiresPermissions("crm:gwai710:init")
    @ResponseBody
    public ResponseData getGwcsdocno(@DtoParam DtoWapper dtoWapper) {
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto inDto = dtoWapper.getDto();
        inDto.put("ENT", shiroUser.getDBC());
        inDto.put("LANG", LangUtils.getLang(shiroUser.getLANG()));
        Dto addDataDto = gwcsTService.getGwcsdocno(inDto);
        return new SuccessResponseData(addDataDto);
    }

    /**
     * 查询产品税种单价信息列表
     */
    @RequestMapping("/listGwam")
    @ResponseBody
    public ResponseData listGwam(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto inDto = dtoWapper.getDto();
        inDto.put("GWAMENT", shiroUser.getDBC());
        inDto.put("lang", LangUtils.getLang(shiroUser.getLANG()));
        List<Map<String, Object>> result = gwcsTService.listGwam(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 查询税种开窗信息列表
     */
    @RequestMapping("/getGweaList")
    @ResponseBody
    public ResponseData getGweaList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWEAENT", shiroUser.getDBC());
        List<Map<String, Object>> result = gwcsTService.getGweaList(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 查询税种开窗信息列表
     */
    @RequestMapping("/getUserList")
    @ResponseBody
    public ResponseData getUserList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("ent", shiroUser.getDBC());
        List<Map<String, Object>> result = gwcsTService.getUserList(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 异步获取折扣率数据
     */
    @ResponseBody
    @RequestMapping("getGwcs035")
    public ResponseData getGwcs035(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto list = gwcsTService.getGwcs035(inDto);
        return new SuccessResponseData(list);
    }

    /**
     * 异步获取汇率数据
     */
    @ResponseBody
    @RequestMapping("getGwcs034")
    public ResponseData getGwcs034(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto list = gwcsTService.getGwcs034(inDto);
        return new SuccessResponseData(list);
    }

    /**
     * 查询购物车信息列表
     */
    @RequestMapping("/getGwcqList")
    @ResponseBody
    public ResponseData getGwcqList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Page<Object> page = dtoWapper.getPage();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWCQENT", shiroUser.getDBC());
        inDto.put("lang", LangUtils.getLang(shiroUser.getLANG()));
        List<Map<String, Object>> result = gwcsTService.getGwcqList(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 获取产品分类列表
     */
    @ResponseBody
    @RequestMapping("/getGwahList")
    public Dto getGwahList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWAHENT", shiroUser.getDBC());
        inDto.put("lang", LangUtils.getLang(shiroUser.getLANG()));
        List<Map<String, Object>> list = gwamTService.getGwahList(inDto);
        outDto.put("list", list);
        return outDto;
    }

    /**
     * 获取产品分类列表
     */
    @ResponseBody
    @RequestMapping("/getGwaoList")
    public Dto getGwaoList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWAOENT", shiroUser.getDBC());
        inDto.put("lang", LangUtils.getLang(shiroUser.getLANG()));
        List<Map<String, Object>> list = gwamTService.getGwaoList(inDto);
        outDto.put("list", list);
        return outDto;
    }

    /**
     * 获取产品分类列表
     */
    @ResponseBody
    @RequestMapping("/getGwabList")
    public Dto getGwabList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWABENT", shiroUser.getDBC());
        inDto.put("lang", LangUtils.getLang(shiroUser.getLANG()));
        List<Map<String, Object>> list = gwamTService.getGwabList(inDto);
        outDto.put("list", list);
        return outDto;
    }

    /**
     * 获取属性特征分类列表
     */
    @ResponseBody
    @RequestMapping("/getGwaiList")
    public Dto getGwaiList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWAIENT", shiroUser.getDBC());
        inDto.put("lang", LangUtils.getLang(shiroUser.getLANG()));
        List<Map<String, Object>> list = gwamTService.getGwaiList(inDto);
        outDto.put("list", list);
        return outDto;
    }

    /**
     * 获取属性特征值列表
     */
    @ResponseBody
    @RequestMapping("/getGwajList")
    public Dto getGwajList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto = Dtos.newDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWAJENT", shiroUser.getDBC());
        inDto.put("lang", LangUtils.getLang(shiroUser.getLANG()));
        List<Map<String, Object>> list = gwamTService.getGwajList(inDto);
        outDto.put("list", list);
        return outDto;
    }

    /**
     * 修改订单状态
     */
    @RequestMapping("/updateData")
    @RequiresPermissions("crm:gwai710:init")
    @ResponseBody
    public ResponseData updateData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwcsT> gwcsTList = inDto.getReqList(GwcsT.class);
        gwcsTService.updateData(gwcsTList);
        return SUCCESS_TIP;
    }

    /**
     * 转ERP订单
     */
    @RequestMapping("/transfer")
    @RequiresPermissions("crm:gwai710:init")
    @ResponseBody
    public ResponseData transfer(@DtoParam DtoWapper dtoWapper) {
        Dto dDto = dtoWapper.getDto();
        JSONArray json = new JSONArray();
        try {
            json = erpLogic.getERPService("transfer710",dDto);
        }catch (NoSuchMethodException e) {
            e.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }
        return ResponseData.success(json);

    }

    public List<Dto> listGwcs032(Dto dto) {
        List<Dto> pList = gwcsTService.listGwcs032(dto);
        return pList;
    }

    public List<Dto> listGwcsInfo(Dto dto) {
        List<Dto> pList = gwcsTService.listGwcsInfo(dto);
        return pList;
    }

    public Dto getGwakData(Dto dto) {
        Dto info = this.gwakTService.getGwakData(dto);
        return info;
    }

    /**
     * 客户资料同步
     */
    public String SyncERP(String gwak001, String MB001) {
        ShiroUser shiroUser = ShiroKit.getUser();

        //获取gwak001对应的客户资料数据
        Dto qDto = Dtos.newDto();
        qDto.put("ent", shiroUser.getDBC());
        qDto.put("gwak001", gwak001);
        Dto info = this.gwakTService.getGwakData(qDto);

        //获取目标数据库
//        Dto dbinfo = this.gwakTService.getDBinfo(MB001);
        Dto outDto = Dtos.newDto();
//        outDto.put("MB001", dbinfo.getString("MB001").trim());//获取目标数据库
        outDto.put("MB001", info.getString("GWAKPLANT"));
        outDto.put("MA001", info.getString("GWAK001"));
        outDto.put("MA002", info.getString("GWAKL003"));
        outDto.put("MA003", info.getString("GWAKL004"));
        outDto.put("MA004", info.getString("GWAK050"));
        outDto.put("MA005", info.getString("GWAK031"));//联系人
        outDto.put("MA006", info.getString("GWAK030"));//电话1
//                    outDto.put("MA007", info.getString("GWAK050"));//电话2
//        outDto.put("MA010", info.getString("GWAK007"));
        if (info.getString("GWAK051").equals("NULL")) {
            outDto.put("MA011", "");
        } else {
            outDto.put("MA011", info.getBigDecimal("GWAK051"));
        }
        if (info.getString("GWAK052").equals("NULL")) {
            outDto.put("MA012", "");
        } else {
            outDto.put("MA012", info.getBigDecimal("GWAK052"));
        }
        outDto.put("MA014", info.getString("GWAK008"));
        outDto.put("MA017", info.getString("GWAK023"));
        outDto.put("MA018", info.getString("GWAK024"));
        outDto.put("MA019", info.getString("GWAK021"));
        outDto.put("MA023", info.getString("GWAK028"));//注册地址
        if (!(info.getString("GWAL018_fp").isEmpty())) {
            outDto.put("MA025", info.getString("GWAL018_fp"));//gwal018 where gwal003='I' 发票地址
        }
        if (!(info.getString("GWAL018_sh").isEmpty())) {
            outDto.put("MA027", info.getString("GWAL018_sh"));//gwal018 where gwal003='D' 送货地址
        }
        outDto.put("MA031", info.getString("GWAK060_NAME"));
        outDto.put("MA032", "N");
        outDto.put("MA033", 0);
        outDto.put("MA034", 0);
        outDto.put("MA035", "2");
        outDto.put("MA036", 0);
        outDto.put("MA037", "A");
//        outDto.put("MA038", info.getString("GWAK012"));//税种
        outDto.put("MA038", "1");//税种
        outDto.put("MA039", "1");
        outDto.put("MA041", "2");
        outDto.put("MA042", "1");
        outDto.put("MA044", 0);
        outDto.put("MA045", 0);
        outDto.put("MA047", "112201"); //账款科目
//        outDto.put("MA048", "1");
        outDto.put("MA059", 0);
        outDto.put("MA060", 0);
        outDto.put("MA061", 0);
        outDto.put("MA062", "");//地址
        outDto.put("MA066", "N");
        outDto.put("MA067", 0);
        outDto.put("MA074", "1121");
        outDto.put("MA075", "86");
        outDto.put("MA082", "N");
        outDto.put("MA083", info.getString("GWAK060"));//付款条件
        outDto.put("MA084", "N");
        outDto.put("MA086", "N");
        outDto.put("MA087", "2");
        outDto.put("MA088", "1");
        outDto.put("MA089", "1");
        outDto.put("MA090", 1);
        outDto.put("MA091", 1);
        outDto.put("MA092", 1);
        outDto.put("MA093", 1);
        outDto.put("MA094", 1);
        outDto.put("MA095", 0);
        outDto.put("MA096", "1");
        outDto.put("MA097", "1");
        //抓税率
        if (info.getString("GWEA006") == null || info.getString("GWAK004").isEmpty()) {
            outDto.put("MA101", 0.16);
        } else {
            outDto.put("MA101", info.getBigDecimal("GWEA006"));
        }
        Dto orgDto = this.getSysOrg(shiroUser.getORG_ID());
        outDto.put("MA103", "1");
        outDto.put("MA104", 0);
        outDto.put("MA105", "220301");
        outDto.put("MA106", "N");
        outDto.put("MA109", 0);
        outDto.put("MA110", 0);
        outDto.put("MA111", 0);
        outDto.put("MA113", "N");
//        outDto.put("UDF01", info.getString("GWAK004"));
        outDto.put("COMPANY", info.getString("GWAKPLANT"));

        if (orgDto != null) {
//            outDto.put("COMPANY", orgDto.getString("BIZ_CODE"));
//            outDto.put("USR_GROUP", orgDto.getString("BIZ_CODE"));
        }
//        outDto.put("CREATOR", shiroUser.getBIZ_CODE());
//        outDto.put("CREATE_DATE", DateUtils.getDate("yyyyMMddSSSSS"));
        outDto.put("FLAG", 1);

        //暂时直接写入Y
        //String result = "Y";
        String result = "N";
        //将客户资料插入指定ERP数据库

        int i = this.gwakTService.insertERP(outDto);
        if (i > 0) {
            result = "Y";
        }
        return result;
    }

    public Dto getSysOrg(String str) {
        Dto orgDto = this.gwcsTService.getSysOrg(str);
        return orgDto;
    }

    /**
     * 还原ERP订单
     */
    @RequestMapping("/restore")
    @RequiresPermissions("crm:gwai710:init")
    @ResponseBody
    public ResponseData restore(@DtoParam DtoWapper dtoWapper) {
        Dto dDto = dtoWapper.getDto();
        Dto outDto = Dtos.newDto();
        try {
            outDto = erpLogic.getERPService("restore710",dDto);
        }catch (NoSuchMethodException e) {
            e.printStackTrace();
        }catch(Exception e){
            e.printStackTrace();
        }
        return ResponseData.success(outDto);
    }

    /**
     * 查询活动信息列表
     */
    @RequestMapping("/listGwdc001")
    @ResponseBody
    public ResponseData listGwdc001(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto inDto = dtoWapper.getDto();
        inDto.put("GWCSENT", shiroUser.getDBC());
        inDto.put("lang", LangUtils.getLang(shiroUser.getLANG()));
        List<Map<String, Object>> result = gwcsTService.listGwdc001(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 查询产品信息列表
     */
    @RequestMapping("/listGwda003")
    @ResponseBody
    public ResponseData listGwda003(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto inDto = dtoWapper.getDto();
        inDto.put("GWCSENT", shiroUser.getDBC());
        inDto.put("lang", LangUtils.getLang(shiroUser.getLANG()));
        inDto.put("TYPE", shiroUser.getTYPE());
        List<Map<String, Object>> result = gwcsTService.listGwda003(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 查询产品信息列表
     */
    @RequestMapping("/getPrice")
    @RequiresPermissions("crm:gwai710:init")
    @ResponseBody
    public ResponseData getPrice(@DtoParam DtoWapper dtoWapper) {
        Dto outDto = Dtos.newDto();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result2 = new ArrayList<>();
        Page<Object> page = dtoWapper.getPage();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWCSENT", shiroUser.getDBC());
        inDto.put("lang", LangUtils.getLang(shiroUser.getLANG()));
        Dto result = Dtos.newDto();
        result2 = gwcsTService.getPrice(inDto);
        return ResponseData.success(result2);
    }

    /**
     * 查询产品信息列表
     */
    @RequestMapping("/getGwdc020_021")
    @RequiresPermissions("crm:gwai710:init")
    @ResponseBody
    public ResponseData getGwdc020_021(@DtoParam DtoWapper dtoWapper) {
        Dto outDto = Dtos.newDto();
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWCSENT", shiroUser.getDBC());
        outDto = gwcsTService.getGwdc020_021(inDto);
        return ResponseData.success(outDto);
    }

    /**
     * 动态删除赠品信息
     */
    @RequestMapping("/delGwcszByGwcsz007")
    @RequiresPermissions("crm:gwai710:init")
    @ResponseBody
    public ResponseData delGwcszByGwcsz007(@DtoParam DtoWapper dtoWapper) {
        Dto outDto = Dtos.newDto();
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWCSZENT", shiroUser.getDBC());
        gwcsTService.delGwcszByGwcsz007(inDto);
        return ResponseData.success(outDto);
    }

    /**
     * 异步获取当前经销商协议档数据
     */
    @RequestMapping("/getGwdd")
    @RequiresPermissions("crm:gwai710:init")
    @ResponseBody
    public ResponseData getGwdd(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWDDENT", shiroUser.getDBC());
        Dto rt = gwcsTService.getGwdd(inDto);
        return ResponseData.success(rt);
    }

    /**
     * 查询赠品列表数据
     */
    @RequestMapping("/getGiftInfoData")
    @ResponseBody
    public ResponseData getGiftInfoData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = gwcsTService.getGiftInfoData(inDto);
        return new SuccessResponseData(result);
    }

    /**
     * 查询活动列表数据
     */
    @RequestMapping("/getGwdcOpenData")
    @ResponseBody
    public ResponseData getGwdcOpenData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = gwcsTService.getGwdcOpenData(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 查询赠品开窗列表数据
     */
    @RequestMapping("/getGwdczOpenData")
    @ResponseBody
    public ResponseData getGwdczOpenData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = gwcsTService.getGwdczOpenData(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 添加赠品数据
     */
    @RequestMapping("/addGwcszData")
    @ResponseBody
    public ResponseData addGwdczData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        JSONObject jsonObject = inDto.getReqJsonObject();
//        List<GwcszT> gwcszTList = inDto.getReqList(GwcszT.class);
        List<GwcszT> gwcszTList = jsonObject.parseArray(jsonObject.getString("listData"), GwcszT.class);
        inDto.put("GWCSDOCNO", jsonObject.getString("GWCSDOCNO"));
        gwcsTService.addGwcszData(gwcszTList, inDto);
        return SUCCESS_TIP;
    }

    /**
     * 查询活动对应赠品信息
     */
    @RequestMapping("/getGwcsz005_006")
    @RequiresPermissions("crm:gwai710:init")
    @ResponseBody
    public ResponseData getGwcsz005_006(@DtoParam DtoWapper dtoWapper) {
        Dto outDto = Dtos.newDto();
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWCSENT", shiroUser.getDBC());
        outDto = gwcsTService.getGwcsz005_006(inDto);
        return ResponseData.success(outDto);
    }

    @RequestMapping("/getGwdc020_021_jieti")
    @RequiresPermissions("crm:gwai710:init")
    @ResponseBody
    public ResponseData getGwdc020_021_jieti(@DtoParam DtoWapper dtoWapper) {
        Dto outDto = Dtos.newDto();
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWCSENT", shiroUser.getDBC());
        outDto = gwcsTService.getGwdc020_021_jieti(inDto);
        return ResponseData.success(outDto);
    }

    /**
     * 查询活动是组合|非组合
     */
    @RequestMapping("/getGwcs111")
    @RequiresPermissions("crm:gwai710:init")
    @ResponseBody
    public ResponseData getGwcs111(@DtoParam DtoWapper dtoWapper) {
        Dto outDto = Dtos.newDto();
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWCSENT", shiroUser.getDBC());
        outDto = gwcsTService.getGwcs111(inDto);
        return ResponseData.success(outDto);
    }

    /**
     * check返利类型是否符合条件
     */
    @RequestMapping("/checkGwcs117")
    @RequiresPermissions("crm:gwai710:init")
    @ResponseBody
    public ResponseData getGwcs117(@DtoParam DtoWapper dtoWapper) {
        Dto outDto = Dtos.newDto();
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWDDENT", shiroUser.getDBC());
        outDto = gwcsTService.checkGwcs117(inDto);
        return ResponseData.success(outDto);
    }

    /**
     * 查询最畅销产品信息
     */
    @RequestMapping("/getMaxSaleProduct")
    @ResponseBody
    public ResponseData getMaxSaleProduct(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = gwcsTService.getMaxSaleProduct(inDto);
        return ResponseData.success(result);
    }

    /**
     * 查询最新活动产品信息
     */
    @RequestMapping("/getOnSaleProduct")
    @ResponseBody
    public ResponseData getOnSaleProduct(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = gwcsTService.getOnSaleProduct(inDto);
        return ResponseData.success(result);
    }

    /**
     * 查询非组合产品的阶梯价格
     */
    @RequestMapping("/getPriceSingel")
    @RequiresPermissions("crm:gwai710:init")
    @ResponseBody
    public ResponseData getPriceSingel(@DtoParam DtoWapper dtoWapper) {
        Dto outDto = Dtos.newDto();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result2 = new ArrayList<>();
        Page<Object> page = dtoWapper.getPage();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWCSENT", shiroUser.getDBC());
        inDto.put("lang", LangUtils.getLang(shiroUser.getLANG()));
        Dto result = Dtos.newDto();
        result = gwcsTService.getPriceSingel(inDto);
        return ResponseData.success(result);
    }

    /**
     * 更新仓库
     */
    @RequestMapping("/updateGWCS113")
    @RequiresPermissions("crm:gwai710:init")
    @ResponseBody
    public ResponseData updateGWCS113(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        gwcsTService.updateGWCS113(inDto);
        return SUCCESS_TIP;
    }

    @RequestMapping(value = "/importsData", method = RequestMethod.POST)
    @RequiresPermissions("crm:gwai710:init")
    @ResponseBody
    public ResponseData importsData(MultipartFile file, ModelMap map) {
        ShiroUser user = ShiroKit.getUser();
        Dto inDto = Dtos.newDto();
        ImportParams params = new ImportParams();
        params.setTitleRows(0);//表格标题行数,默认0
        params.setHeadRows(1);//表头行数,默认1
        params.setStartRows(0);//字段真正值和列标题之间的距离 默认0
        int count = 0;
        try {
            List<GwcsT> list = ExcelImportUtil.importExcel(file.getInputStream(), GwcsT.class, params);
            //创建临时表
            String tableName = "##LS" + getCharAndNumr(5);//临时表名
            switch(AppCxt.getParam("ERP")){
                case "T100":
                    tableName=tableName.substring(2);
                    break;
                default:
                    tableName=tableName;
                    break;
            }
            this.gwcsTServiceImpl.createTable(tableName);
            this.gwcsTServiceImpl.insertLS(list, tableName);
            Dto lsQuery = Dtos.newDto();
            lsQuery.put("tableName", tableName);
            List<Dto> ls_list = this.gwcsTServiceImpl.getLsByGroup(tableName);
            switch(AppCxt.getParam("ERP")){
                case "T100":
                    this.gwcsTServiceImpl.dropTable(tableName);
                    break;
                default:
                    break;
            }
            if (ls_list == null) {
                return new ErrorResponseData(401,
                        "所有导入数据都不符合要求!");
            } else {
                this.gwcsTServiceImpl.insertGwcs(tableName, ls_list);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ErrorResponseData(BizExceptionEnum.SERVER_ERROR.getCode(),
                    BizExceptionEnum.SERVER_ERROR.getMessage());
        }
        return SUCCESS_TIP;
    }

    /**
     * java生成随机数字和字母组合
     */
    public static String getCharAndNumr(int length) {
        String val = "";
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            // 输出字母还是数字
            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            // 字符串
            if ("char".equalsIgnoreCase(charOrNum)) {
                // 取得大写字母还是小写字母
                int choice = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char) (choice + random.nextInt(26));
            } else if ("num".equalsIgnoreCase(charOrNum)) { // 数字
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }

    @RequestMapping("/getGwcsdocnoData")
    @ResponseBody
    public ResponseData getGwcsdocnoData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Page<Object> page = dtoWapper.getPage();
        List<Map<String, Object>> result = gwcsTService.getGwcsdocnoData(page, inDto);
        return new PageReData(page, result);
    }

}
